using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP.Commons;
namespace WP.Modules
{
    public partial class frmDialPhone : BaseQryRpt 
    {
        public frmDialPhone()
        {
            InitializeComponent();
            loginUtil = new LoginUtil();
            mstrLogFile = loginUtil.GetResourcedir(false) + Constants.LOGFILE_NAME;
        }
        private LoginUtil loginUtil;
        private string  mstrLogFile="",mstrCustNo="",mstrSupNo="";
        private void frmDialPhone_Load(object sender, EventArgs e)
        {
            string[] sPort = System.IO.Ports.SerialPort.GetPortNames();
            if (sPort.Length > 0)
            {
                for (int i = sPort.Length-1; i >=0; i--)
                {
                    cboComPort.Items.Add(sPort[i]);
                }
            }
            else
            {
                cboComPort.Items.Add("COM1");
                cboComPort.Items.Add("COM2");
                cboComPort.Items.Add("COM3");
                cboComPort.Items.Add("COM4");
            }
            
            cboComPort.Text = loginUtil.GetInIString("AutoDial", "Com", mstrLogFile);
            if(cboComPort.Text.Trim()=="") cboComPort.SelectedIndex = 0;
            //txtPhoneNo.Text = "88615143";
            ////txtPhoneNo.Text = ""; 

        }

        private void btnBreak_Click(object sender, EventArgs e)
        {
            if (this.serialPort1.IsOpen)
            {
                this.serialPort1.Write ("ATH" + "\r");
                this.serialPort1.Close(); 
            }
            this.btnCall.Enabled = true;
            this.btnBreak.Enabled = false;
        }

        private void btnCall_Click(object sender, EventArgs e)
        {
            try
            {
                loginUtil.WriteIniString("AutoDial", "Com", this.cboComPort.Text, mstrLogFile);
                if (this.serialPort1.IsOpen) this.serialPort1.Close(); 
                string DialString = "ATDT" + txtPhoneNo.Text + ";" + "\r";
                this.serialPort1.PortName = this.cboComPort.Text;
                this.serialPort1.BaudRate = 9600;
                this.serialPort1.Parity = System.IO.Ports.Parity.None;
                this.serialPort1.DataBits = 8;
                this.serialPort1.StopBits = System.IO.Ports.StopBits.One;
                if (!this.serialPort1.IsOpen) this.serialPort1.Open();
                this.serialPort1.DiscardInBuffer();
                this.serialPort1.DiscardOutBuffer();
                this.serialPort1.Write(DialString);
                this.btnCall.Enabled = false;
                this.btnBreak.Enabled = true;
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message ,SysVar.WarnTitle);
            }
            
        }

        public void DialCustPhone(string strCustNo,string strPhoneNo)
        {
            mstrCustNo = strCustNo;
            txtPhoneNo.Text = strPhoneNo;
            this.ShowDialog(); 
        }
        public void DialSupPhone(string strSupNo, string strPhoneNo)
        {
            mstrSupNo = strSupNo;
            txtPhoneNo.Text = strPhoneNo;
            this.ShowDialog();
        }


        protected override void ubtLookUp_Click(object sender, EventArgs e)
        {
            Control octrol = sender as Control;
            Control con = null;
            string strSQL = "", strFindValue = "", strLookUpField = "", strCaption = "", strCond = "", strCondition = "";

            if (mstrCustNo!="" )
            {
                strSQL = "select Contact,Phone,Fax,EmailAddress,PhoneExt,CellPhoneNo,Job,Department from arcontact where CustNo='" + mstrCustNo + "'";
                strCaption = "Contact";
                strFindValue = txtPhoneNo.Text;
                strLookUpField = "Phone";
                con = txtPhoneNo;
            }
            else if (mstrSupNo != "")
            {

            }
           if (mblnLookUpSortByDescription) strSQL = DataFunctions.GetSqlByDescriptionSort(strSQL);
            frmLookUp frmlookup = new frmLookUp(strSQL, strCaption, strFindValue, strLookUpField);

            frmlookup.ShowDialog();
            if (frmlookup.HaveReturnValue)
            {
                if (con != null)
                {
                    con.Text = frmlookup.GetReturnValue(strLookUpField);
                }
            }
            frmlookup.Close();
            con.Focus();

        }

    }
}